package com.scopionstudio.newpaper;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.scopionstudio.adapter.PaperAdapter;
import com.scopionstudio.bean.Variables;
import com.scopionstudio.network.CheckNetWork;

public class MainMusicActivity extends ScopionActivity implements OnClickListener, OnItemClickListener {
	ListView mList;
	 //private ArrayList<String> mIdList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.testactivity);
		mList = (ListView) findViewById(R.id.lstView);
		mList.setOnItemClickListener(this);
		PaperAdapter objPaper = new PaperAdapter(this, R.id.tvHello, Variables.PAPERS);
		mList.setAdapter(objPaper);
		/* if (mIdList == null) {
		      mIdList = new ArrayList<String>();
		    }*/
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
		// TODO Auto-generated method stub
		try{
			CheckNetWork objCheck = new CheckNetWork(getApplicationContext());
			boolean check = objCheck.isOnline();
			if(check){
				//new MainAsyncTask().execute(position);
				Intent intent = new Intent(MainMusicActivity.this,CategoryActivity.class);
				intent.putExtra(Variables.PAPER, position);
				startActivity(intent);
				finish();
			}
			else {
				showAlertDialog(getParent(), "Không có kết nối",
	                   "Máy bạn chưa có kết nối mạng, xin hãy kiểm tra lại !", false);
				//Intent objEnableInternet = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
			}
		}catch(Exception e){
			Log.e("Mainactivity", e.toString());
		}
		
	}
	  public void showAlertDialog(Context context, String title, String message, Boolean status) {
	        AlertDialog alertDialog = new AlertDialog.Builder(context).create();
	        alertDialog.setTitle(title);
	        alertDialog.setMessage(message);
	        alertDialog.setIcon((status) ? R.drawable.checktrue : R.drawable.checkfalse);
	        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
	            public void onClick(DialogInterface dialog, int which) {
	            }
	        });
	        alertDialog.show();
	    }

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}
	@Override
	public void onBackPressed() {
		AlertDialog.Builder mAlert = new AlertDialog.Builder(
				MainMusicActivity.this);
		mAlert.setTitle("Thông báo thoát");
		mAlert.setMessage(
				"Bạn có thật sự muốn thoát khỏi chương trình không ?")
				.setPositiveButton("Có",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}
						})
				.setNegativeButton("Không",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.cancel();
								dialog.dismiss();
							}
						});
		AlertDialog mAlertDialog = mAlert.create();
		mAlertDialog.show();
	}
}